package com.polaris.live.viewmodel.home

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.net.gameService
import com.polaris.live.net.homeService
import com.polaris.live.resp.back_resp.CommunityTranslationResp
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import com.polaris.live.resp.go_resp.DynamicTranslateRequest
import com.polaris.live.resp.go_resp.IdRequest
import com.polaris.live.resp.go_resp.ReleaseDynamicRequest
import com.polaris.live.widget.refresh.OnListListener
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

/**
 * DynamicViewModel
 *
 * @author Created by 半仙 on 2024/9/4/004 9:29
 */
class DynamicViewModel : BaseViewModel(), OnListListener {

    val mListLiveData = MutableLiveData<ResultState<ListResp<Any>>>()

    private var pageSize = 20
    private var bannerPosition = 1

    private fun getCommunityList(position: Int) {
        request({
            if (position == 0) {
                coroutineScope {
                    val listResp = async { gameService.getCommunityList(position, pageSize) }.await()
                    val bannerResp =
                        async { homeService.getBannerListV2(CommonConst.BannerType.BANNER_DYNAMIC) }.await()

                    val list = listResp.result?.list?.toMutableList<Any>()?.apply {
                        val bannerBean = ListWithBannerBean(bannerResp.result?.banner ?: emptyList())
                        if (bannerPosition >= size) {
                            add(bannerBean)
                        } else {
                            add(bannerPosition, bannerBean)
                        }
                    } ?: emptyList()

                    if (CollectionUtils.isEmpty(bannerResp.result?.banner)) {
                        listResp.new(listResp.result?.list)
                    } else {
                        listResp.new(list)
                    }
                }
            } else {
                gameService.getCommunityList(position, pageSize).let { it.new(it.result?.list) }
            }
        }, mListLiveData)
    }

    override fun onRefresh() {
        getCommunityList(0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getCommunityList(pageIndex)
    }

    //点赞
    val idLiveData = MutableLiveData<Pair<Long, Int>>()
    fun communityLike(id: Long, position: Int) {
        request({ gameService.communityLike(IdRequest(id)) }, {
            idLiveData.value = Pair(id, position)
        })
    }

    //发布动态
    val sendDynamicLiveData = MutableLiveData<ResultState<Any>>()
    fun releaseDynamic(content: String, picUrl: String? = null) {
        request({ gameService.releaseDynamic(ReleaseDynamicRequest(content, picUrl)) }, sendDynamicLiveData)
    }

    //翻译
    val translateLiveData = MutableLiveData<Pair<CommunityTranslationResp, Int>>()
    fun translateText(position: Int, id: Long, locale: String) {
        request({ gameService.communityTranslate(DynamicTranslateRequest(locale, id)) }, {
            translateLiveData.value = Pair(it, position)
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }
}